// pages/charge/charge.js
const app = getApp();
const $api = require('../../api/index');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        navTop: app.globalData.navTop,
        search: '', //搜索栏
        active: 0, //选中的切换节点,
        isLoading: true,
        dataList: [],
        userLevel: ['', '普通', '银牌', '金牌', '钻石'],
        page: 1,
        pageSize: 10
    },
    bindKeyInput: function (e) {
        this.setData({
            search: e.detail.value
        })
    },
    openMap(e) { //打开地图
        const latitude = parseFloat(e.mark.posY)
        const longitude = parseFloat(e.mark.posX)
        const name = e.mark.name
        const address = e.mark.address
        wx.openLocation({
            latitude,
            longitude,
            scale: 18,
            name: name,
            address: address
        })
    },
    gotoDetail({
        mark
    }) { //跳转详情页
        wx.navigateTo({
            url: '../chargeDetail/changeDetail?id=' + mark.id
        })
    },
    getList() { //电站列表
        if(!this.data.isLoading){
            return;
        }
        let data = {
            longitude: wx.getStorageSync('longitude') || '',
            latitude: wx.getStorageSync('latitude') || '',
            name: this.data.search,
            page: this.data.page,
            pageSize: this.data.pageSize
        }
        $api.orgList(data).then((res) => {
            res.data.map(list => {
                let tag = []
                list.tags.map(tags => {
                    tag.push(tags.name)
                })
                list.price_list.map(price => {
                    price.totalPrice = (Number(price.price) + Number(price.service_price)).toFixed(4)
                })
                list.tag = tag.join(' 丨 ')
                list.distance = (list.distance / 1000).toFixed(2)
            })

            if (this.data.page == 1) {
                this.setData({
                    dataList: res.data,
                    "param.page": this.data.page += 1
                })
            } else {
                let dataList = this.data.dataList.concat(res.data);
                this.setData({
                    dataList,
                    "page": this.data.page += 1
                })
            }
            if (res.data && res.data.length < 10) {
                this.setData({
                    isLoading: false
                })
            }
        })
    },
    toFixeds(val) {
        return val.toFixed(2)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        this.getList()
    },
    searchList() {
      this.setData({
          page:1
      });
      this.getList();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: 1
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getList();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})